<?php

//小站选项应用模型Special模块
class Special_model extends Model {

	function Special_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("plugins_model", "pm");
		$config['upload_path'] = './data/upload_pic';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['file_name'] = strtotime(date('Y-m-d H:i:s'));
		$config['max_size'] = '100';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$this->load->library('upload', $config);
	}
	
	//获取列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("module_special")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach($items as $k => $list){
				$siteid = $this->pm->get_siteid($list["plid"]);
				$site = $this->sm->get($siteid);
				if($site) {
					$items[$k]["domain"] = $site["domain"];
					$items[$k]["sitename"] = $site["name"];
					$items[$k]["logo"] = $site["logo"];
				} else {
					$items[$k]["domain"] = $items[$k]["sitename"] = $items[$k]["logo"] = "";
				}
				
				//用户信息
				$userid = $list["userid"];
				$userinfo = $this->am->get($userid);
				$items[$k]["userinfo"] = $userinfo;
			}
		}
		return $items;
	}
	
	//计算总数
	function countitems()
	{
		return $this->db->count_all_results("module_special");
	}
	
	//处理功能
	function add_special($data = array())
	{
		if(count($data) == 0) return false;
		//添加记录
		$insertdata = array(
			"typeid" => $data["typeid"],
			"sid" => $data["dataid"],
			"userid" => $data["userid"],
			"jointime" => time(),
		);
		$this->db->insert("module_special_val", $insertdata);
		
		//更新记录
		$query = $this->db->get_where("module_special", array("id"=>$data["dataid"]));
		$row = $query->row_array();
		$viewcount1 = $row["viewcount1"];
		$viewcount2 = $row["viewcount2"];
		if($data["typeid"] == 0){
			$counts = $viewcount1+1;
			$updatedata =  array("viewcount1"=>$counts);
		} else {
			$counts = $viewcount2+1;
			$updatedata =  array("viewcount2"=>$counts);
		}
		$this->db->where("id", $data["dataid"]);
		$this->db->update("module_special", $updatedata);
		return $counts;
	}
	
	//检测是否参与过
	function check_special($sid, $userid)
	{
		$query = $this->db->get_where("module_special_val", array("sid"=>$sid, "userid"=>$userid));
		if ($query->num_rows()>0) {
			return true;
		}
		return false;
	}
	
	//添加
	function add($plid)
	{
		//图片上传
		$pic = ciupload("pic");
		if($pic != "") {//图片剪切
			$params = array('src_file' => './data/upload_pic/'.$pic, 'dst_file' => './data/upload_pic/'.$pic);
			//$this->load->library("imagecrop", $params);
			$this->load->library("imagecrop");
			$this->imagecrop->src_file=$params['src_file'];
			$this->imagecrop->dst_file=$params['dst_file'];
			$this->imagecrop->LoadImage();
			$this->imagecrop->Crop(200,150,1);
			$this->imagecrop->SaveImage();
			$this->imagecrop->destory();
		}
	  	
		$data = array(
			"userid" => $this->userid,
			"title" => p("title"),
			"description" => p("description"),
			"plid" => $plid,
			"pic" => $pic,
			"viewpoint1" => p("viewpoint1"),
			"viewpoint2" => p("viewpoint2"),
			"addtime" => time(),
		);
		$result = $this->db->insert("module_special", $data);
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}
	
	//编辑
	function edit($plid)
	{
		$id = p("id");
		//图片上传
		$pic = ciupload("pic");
		if($pic != "") {//图片剪切
			$params = array('src_file' => './data/upload_pic/'.$pic, 'dst_file' => './data/upload_pic/'.$pic);
			//$this->load->library("imagecrop", $params);
			$this->load->library("imagecrop");
			$this->imagecrop->src_file=$params['src_file'];
			$this->imagecrop->dst_file=$params['dst_file'];
			$this->imagecrop->LoadImage();
			$this->imagecrop->Crop(200,150,1);
			$this->imagecrop->SaveImage();
			$this->imagecrop->destory();
		}
		$data = array(
			"userid" => $this->userid,
			"title" => p("title"),
			"description" => p("description"),
			"plid" => $plid,
			"viewpoint1" => p("viewpoint1"),
			"viewpoint2" => p("viewpoint2"),
			"addtime" => time(),
		);
		//图片是否更新检测
		if ($pic != "") {
			$data['pic'] = $pic;	
		}
		$this->db->where("id", $id);
		$result = $this->db->update("module_special", $data);
		return $result;
	}
	
	//删除
	function delete($dataid)
	{
		$result = $this->db->delete("module_special", array("id"=>$dataid));
		return $result;
	}
	
	//删除选择
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//删除评论
//		$this->comment_model->forum_delete($id);
		$this->db->where_in("dataid", $delid);
		$this->db->delete("comment");
		
		$this->db->where_in("id", $delid);
		$this->db->delete("module_special");
		return $result;
	}
	
	//#获取列表
	function lists($plid, $limit = 5, $offset = 0)
	{
		$lists = array();
		$query = $this->db->from("module_special")->where(array("plid"=>$plid))->order_by("id","desc")->limit($limit, $offset)->get();
		if($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row) {
				//模块ID
				$plid = $row["plid"];
				$q = $this->db->get_where("site_plugins", array("id"=>$plid));
				$r = $q->row_array();
				$lists[$k]["moduleid"] = $r["moduleid"];
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_lists($plid)
	{
		$query = $this->db->from("module_special")->where(array("plid"=>$plid))->get();
		return $query->num_rows();
	}
	
	//#查看内容
	function show($id)
	{
		$row = array();
		$query = $this->db->get_where("module_special", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			$viewcount1 = $row["viewcount1"];
			$viewcount2 = $row["viewcount2"];
			$total = $viewcount1+$viewcount2;
			if($total>0) {
				$row["scale1"] = sprintf("%01.2f",$viewcount1/$total)*100;
				$row["scale2"] = sprintf("%01.2f",$viewcount2/$total)*100;
			} else {
				$row["scale1"] = 0;
				$row["scale2"] = 0;
			}
			//小站信息
			$r = $this->pm->get($row["plid"]);
			$opid = $r["opid"];
			$r = $this->options_model->get($opid);
			if ($r) {
				$siteid = $r["siteid"];
				$row["siteid"] = $siteid;
				//小站信息
				$row["site"] = $this->sm->get_site($siteid);
			} else {
				$row["site"]["name"] = '';
			}
			//用户信息
			$userid = $row["userid"];
			$userinfo = $this->am->get($userid);
			$row["userinfo"] = $userinfo;
		}
		return $row;
	}
	
	//#查看内容
	function get_info($id)
	{
		$row = array();
		$query = $this->db->get_where("module_special", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			$viewcount1 = $row["viewcount1"];
			$viewcount2 = $row["viewcount2"];
			$total = $viewcount1+$viewcount2;
			if($total>0) {
				$row["scale1"] = sprintf("%01.2f",$viewcount1/$total)*100;
				$row["scale2"] = sprintf("%01.2f",$viewcount2/$total)*100;
			} else {
				$row["scale1"] = 0;
				$row["scale2"] = 0;
			}
		}
		return $row;
	}
	
}

/* End of file */